package com.copro.chemdb.result;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ihelpuoo.lang.NumericUtils;
import org.ihelpuoo.util.Pager;

import com.copro.simple.Application;

public class ListResultAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Application app = Application.me(request);
		int id = NumericUtils.parseInt(request.getParameter("id"), 0);
		int parentId = NumericUtils.parseInt(request.getParameter("parentId"),
				0);
		int type = NumericUtils.parseInt(request.getParameter("type"), 0);
		request.setAttribute("type", type);
		List<Result> results = null;
		if (parentId > 0) {
			if (type == 0) {
				results = ResultUtils.getSubResultList(app, parentId);
			} else {
				results = ResultUtils.getDBQuesSubResultList(app, parentId);
			}
			request.setAttribute("resultSet", results);
			return mapping.findForward("subList");
		} else {
			Pager pager = Pager.me(request);
			pager.setPageSize(50);
			if (type == 0) {
				results = ResultUtils.getResultList(app, id, pager);
			} else {
				results = ResultUtils.getDBQuesResultList(app, id, pager);
			}
			request.setAttribute("resultSet", results);
			request.setAttribute("quesId", new Integer(id));
			request.setAttribute("pager", pager);
			return mapping.findForward("list");
		}
	}

}
